#include "main.h"

/**************************************************************************************/
 InputSta_t knobl;
 InputSta_t knobr;
 InputSta_t   door;
 InputSta_t  doorbake;
 InputSta_t   waterbox;
 InputSta_t  potstate;

TEMPR_t			tempr_box;  //箱体下温度
TEMPR_t			tempr_bom;	//底盘温度
TEMPR_t			tempr_top;	//箱体上温度（加热膜）



bool            zero_flag;      //电源状态     1：过零 
bool            gb_outfireoff;

u8			tempr_wall_L;	//防火墙温度
u8			tempr_wall_R;	//
Wall_t      wall_L,wall_R;
bool        shell_err_f;
u16         fire_L_thcp_ad;
u16         fire_R_thcp_ad;
GanS_t      gans_L;//gans_L,
Boil_t      boli;
u8 boxerr_cnt;
u8 toperr_cnt;
u8 bomerr_cnt;
u8 gans_errcnt;
u8 leak_checkdly;
/**************************************************************************************/
crom u16 NTC_ad_table[] = {
    3975,	3970,	3964,	3958,	3952,	3945,	3939,	3932,	3924,	3917,   //    0 ~   9
    3909,	3901,	3892,	3883,	3873,	3863,	3852,	3841,	3829,	3816,   //   10 ~  19
    3802,	3788,	3772,	3756,	3738,	3719,	3704,	3688,	3672,	3655,   //   20 ~  29
    3637,	3619,	3601,	3582,	3563,	3543,	3522,	3501,	3480,	3457,   //   30 ~  39
    3435,	3412,	3388,	3364,	3339,	3314,	3288,	3262,	3235,	3208,   //   40 ~  49
    3180,	3152,	3124,	3095,	3065,	3035,	3005,	2975,	2944,	2913,   //   50 ~  59
    2881,	2849,	2817,	2785,	2752,	2720,	2687,	2653,	2620,	2586,   //   60 ~  69
    2553,	2519,	2485,	2452,	2417,	2384,	2350,	2316,	2282,	2248,   //   70 ~  79
    2215,	2181,	2147,	2113,	2080,	2047,	2014,	1982,	1949,	1917,   //   80 ~  89
    1885,	1853,	1821,	1790,	1759,	1728,	1698,	1668,	1638,	1608,   //   90 ~  99
    1580,	1550,	1522,	1494,	1466,	1438,	1411,	1385,	1358,	1332,   //   100 ~  109
    1307,	1281,	1256,	1232,	1208,	1184,	1161,	1138,	1115,	1093,   //   110 ~  119
    1071,	1049,	1028,	1008,	987,	967,	947,	928,	909,	890,    //   120 ~  129
    872,	854,	836,	819,	802,	785,	769,	753,	737,	722,    //   130 ~  139
};
/* crom u16 GANS_ad_table[] = {//3K
    4081,	4080,	4079,	4078,	4077,	4076,	4075,	4074,	4073,	4072,   //0-9
    4070,	4069,	4068,	4066,	4065,	4063,	4062,	4060,	4058,	4056,   //10-19
    4054,	4052,	4050,	4048,	4046,	4044,	4041,	4039,	4036,	4033,   //20-29
    4030,	4027,	4024,	4021,	4018,	4014,	4011,	4007,	4003,	3999,   //30-39
    3995,	3991,	3986,	3982,	3977,	3972,	3967,	3962,	3956,	3951,   //40-49
    3945,	3939,	3933,	3926,	3920,	3913,	3906,	3899,	3891,	3884,   //50-59
    3876,	3867,	3859,	3850,	3842,	3832,	3823,	3814,	3804,	3793,   //60-69
    3783,	3772,	3761,	3750,	3738,	3727,	3715,	3702,	3689,	3676,   //70-79
    3663,	3649,	3635,	3621,	3607,	3592,	3576,	3561,	3545,	3529,   //80-89
    3513,	3496,	3479,	3461,	3444,	3426,	3407,	3389,	3370,	3350,   //90-99
    3331,	3311,	3291,	3271,	3250,	3229,	3207,	3186,	3164,	3142,   //100-109
    3120,	3097,	3074,	3051,	3028,	3004,	2980,	2956,	2932,	2908,   //110-119
    2883,	2858,	2833,	2808,	2783,	2758,	2732,	2706,	2680,	2655,   //120-129
    2629,	2602,	2576,	2550,	2524,	2497,	2471,	2444,	2418,	2391,   //130-139
    2365,	2338,	2312,	2285,	2259,	2232,	2206,	2180,	2154,	2128,   //140-149
    2102,	2076,	2050,	2024,	1998,	1973,	1947,	1922,	1897,	1872,   //150-159
    1847,	1823,	1798,	1774,	1750,	1726,	1702,	1678,	1655,	1631,   //160-169
    1609,	1586,	1563,	1541,	1519,	1497,	1475,	1453,	1432,	1411,   //170-179
    1391,	1370,	1349,	1330,	1310,	1290,	1271,	1252,	1233,	1214,   //180-189
    1196,	1177,	1159,	1141,	1124,	1107,	1090,	1073,	1057,	1040,   //190-199
    1024,	1008,	992,	977,	962,	947,	932,	918,	903,	889,    //200-209
    875,	861,	848,	835,	822,	809,	796,	784,	771,	759,    //210-219
    747,	736,	724,	713,	702,	691,	680,	669,	659,	648,    //220-229
    638,	628,	618,	609,	599,	590,	581,	572,	563,	554,    //230-239
    546,	537,	529,	521,	513,	505,	497,	490,	482,	475,    //240-249
    467,	460,	453,	446,	440,	433,	427,	420,	414,	408,    //250-259
    401,	395,	389,	384,	378,	372,	367,	361,	356,	351,    //260-269
    346,	340,	336,	331,	326,	321,	316,	312,	307,	303,    //270-279
    298,	294,	290,	286,	282,	278,	274,	270,	266,	262,    //280-289
    259,	255,	251,	248,	244,	241,	238,	234,	231,	228,    //290-299
    225,	210,	196,	183,	172,	161,	150,	141,	132,	124,    //300-345
    117,	                                                                        //350

};
 */
crom u16 R10K_ad_table[] = {
238,	250,	262,	275,	288,	302,	316,	331,	346,	362,    //0-9
378,	395,	413,	431,	449,	468,	488,	509,	529,	551,    //10-19
573,	596,	619,	643,	668,	693,	719,	745,	772,	799,    //20-29
827,	856,	885,	914,	945,	975,	1006,	1038,	1070,	1103,   //30-39
1136,	1169,	1203,	1237,	1271,	1306,	1341,	1377,	1412,	1448,   //40-49
1484,	1520,	1557,	1593,	1630,	1666,	1703,	1740,	1777,	1813,   //50-59
1850,	1887,	1923,	1960,	1996,	2032,	2068,	2103,	2139,	2174,   //60-69
2209,	2244,	2278,	2312,	2346,	2379,	2413,	2445,	2477,	2509,   //70-79
2541,	2572,	2603,	2633,	2663,	2692,	2721,	2750,	2778,	2806,   //80-89
2833,	2860,	2886,	2912,	2937,	2962,	2986,	3010,	3034,	3057,   //90-99
3080,	3102,	3124,	3145,	3166,	3187,	3207,	3226,	3245,	3264,   //100-109
3283,	3301,	3318,	3336,	3352,	3369,	3385,	3401,	3416,	3431,   //110-119
3446,	3461,	3475,	3489,	3502,	3515,	3528,	3540,	3553,	3565,   //120-129
3576,	3588,   3599,   3610,   3621,   3631,   3641,   3651,   3661,   3670,   //130-139
3679,     
};
crom u16 R510or10K_ad_table[] = {
    525,	539,	552,	566,	581,	595,	610,	625,	640,	655,    //100-109
    671,	687,	703,	719,	735,	752,	769,	786,	803,	821,    //110-119
    838,	856,	874,	893,	911,	930,	949,	968,	987,	1006,   //120-129
    1026,	1045,	1065,	1085,	1105,	1125,	1146,	1166,	1187,	1207,   //130-139
    1228,	1249,	1271,	1292,	1313,	1335,	1356,	1377,	1399,	1421,   //140-149
    1442,	1464,	1486,	1508,	1530,	1552,	1574,	1596,	1618,	1640,   //150-159
    1662,	1684,	1706,	1728,	1750,	1772,	1794,	1815,	1837,	1859,   //160-169
    1881,	1903,	1924,	1946,	1967,	1989,	2010,	2031,	2053,	2074,   //170-179
    2095,	2116,	2136,	2157,	2178,	2198,	2219,	2239,	2259,	2279,   //180-189
    2299,	2319,	2338,	2358,	2377,	2396,	2416,	2435,	2453,	2472,   //190-199
    2491,	2509,	2527,	2545,	2563,	2581,	2598,	2616,	2633,	2650,   //200-209
    2667,	2684,	2701,	2717,	2733,	2750,	2766,	2782,	2797,	2813,   //210-219
    2828,	2843,	2856,	2873,	2888,	2903,	2917,	2931,	2945,	2959,   //220-229
    2973,	2987,	3000,	3014,	3026,	3040,	3053,	3065,	3078,	3090,   //230-239
    3103,	3115,	3126,	3138,	3150,	3162,	3173,	3184,	3195,	3206,   //240-249
    3217,	3228,	3238,	3249,	3259,	3269,	3280,	3289,	3299,	3309,   //250-259
    3319,	3328,	3337,	3346,	3355,	3364,	3373,	3382,	3390,	3399,   //260-269
    3407,	3415,	3424,	3432,	3440,	3447,	3455,	3463,	3470,	3478,   //270-279
    3485,	3492,	3500,	3506,	3514,	3521,	3527,	3534,	3541,	3547,   //280-289  
    3553,   3560,   3566,   3572,   3578,   3584,   3590,   3596,   3602,   3608,   //290-299
    3613, 
    
};
crom u16 bottom2_ad_table[] = {
    129,	136,	143,	150,	157,	165,	173,	181,	190,	199,//0-9
    208,	217,	227,	238,	249,	260,	271,	283,	295,	308,//10-19
    321,	335,	349,	363,	378,	393,	409,	425,	442,	459,//20-29
    477,	495,	514,	533,	553,	573,	593,	614,	636,	658,//30-39
    681,	704,	727,	752,	776,	801,	827,	852,	879,	906,//40-49
    933,	961,	989,	1017,	1046,	1075,	1105,	1135,	1165,	1196,//50-59
    1227,	1258,	1290,	1321,	1353,	1386,	1418,	1451,	1484,	1517,//60-69
    1550,	1583,	1616,	1650,	1683,	1717,	1750,	1784,	1817,	1850,//70-79
    1884,	1917,	1950,	1983,	2016,	2049,	2082,	2115,	2147,	2179,//80-89
    2211,	2243,	2274,	2305,	2336,	2367,	2397,	2427,	2457,	2487,//90-99
    2516,	2544,	2573,	2601,	2629,	2656,	2683,	2710,	2736,	2762,//100-109
    2788,	2813,	2838,	2862,	2887,	2910,	2934,	2957,	2979,	3001,//110-119
    3023,	3045,	3066,	3086,	3107,	3127,	3146,	3166,	3184,	3203,//120-129
    3221,	3239,	3257,	3274,	3290,	3307,	3323,	3339,	3355,	3370,//130-139
    3385,	3399,	3414,	3428,	3441,	3455,	3468,	3481,	3493,	3506,//140-149
    3518,	3530,	3541,	3553,	3564,	3575,	3585,	3596,	3606,	3616,//150-159
    3626,	3635,	3644,	3654,	3663,	3671,	3680,	3688,	3696,	3704,//160-169
    3712,	3720,	3727,	3735,	3742,	3749,	3756,	3762,	3769,	3775,//170-179
    3782,	3788,	3794,	3800,	3806,	3811,	3817,	3822,	3827,	3833,//180-189
    3838,	3843,	3847,	3852,	3857,	3861,	3866,	3870,	3874,	3878,//190-199
    3883,	


};
/**************************************************************************************/
/* 门开关 */
void update_door_state(void)
{
	static u8 ct = 0;
    volatile bool ns;
	static bool door_os = 1; //默认高电平
    ns = GPIO_ReadBit(DOORSTEM_INPIN);
    door_os = get_pin_state2(&ct,ns,door_os);
    if (door_os)
    { //高电平门关    不插器件默认门关
        door.sta = DOOR_CLOSE;
    }else
    {
        door.sta = DOOR_OPEN;
    }
    
}
void update_doorbake_state(void)
{
	static u8 ct = 0;
    volatile bool ns;
	static bool door_os = 1; //默认高电平
    ns = GPIO_ReadBit(DOORBAKE_INPIN);
    door_os = get_pin_state2(&ct,ns,door_os);
    if (door_os)
    { //高电平门关    不插器件默认门关
        doorbake.sta = DOOR_CLOSE;
    }else
    {
        doorbake.sta = DOOR_OPEN;
    }
    
}
void update_pot_state(void)
{
	static u8 ct = 0;
    volatile bool ns;
	static bool door_os = 1; //默认高电平
    static u16 ptime_cnt = 0;
    ns = GPIO_ReadBit(POT_INPIN); 
    door_os = get_pin_state2(&ct,ns,door_os);
    if (door_os)
    { //高电平门关    不插器件默认门关
        potstate.sta = POT_NO;
    }
    else
    {
       potstate.sta = POT_IN;
    }
//    if(potstate.sta == POT_NO)//无锅
//    {
//        if(knobl.sta == KNOB_OPEN)
//        {
//            if(system_100ms_f)
//            {
//                ptime_cnt++;
//                if(ptime_cnt >= 30)//连续检测到3S无锅
//                {
//                    ptime_cnt = 30;
//                    if((gb_ganscheckon == 0)|| (gb_firesmallon == 0)) //判断关闭，无需转小火
//                    {
//                        gb_outfireoff = 0;
//                    }
//                    else
//                    {
//                        gb_outfireoff = 1;
//                    }
//                    //if(sta_Lfire)//有火状态下
//                    {
//                        gans_L.cdly = 50;//干烧延迟5秒检测
//                    }                    
//                }
//            }
//        }
//        else
//        {
//            gb_outfireoff = 1;
//            ptime_cnt = 0;
//        }

//    }
//    else
//    {
//        ptime_cnt = 0;
//        if(knobl.sta == KNOB_OPEN)
//            gb_outfireoff = 0;
//        else
//            gb_outfireoff = 1;
//    }   
}
/* 左旋钮状态输入 */
void update_knob1_state(void)
{
    static u8 ct = 0;
    volatile bool ns;
	static bool knob_os = 1;

    ns = GPIO_ReadBit(KNOB1_INPIN);
    knob_os = get_pin_state2(&ct,ns,knob_os); //100ms
         
    if (knob_os == 1)
        knobl.sta = KNOB_CLOSE;
    else
        knobl.sta = KNOB_OPEN;//

    fire_L.xnsta = knobl.sta;
}

/* 右旋钮状态输入 */
void update_knob2_state(void)
{
    static u8 ct = 0;
    volatile bool ns;
    static bool knob_os = 1;

    ns = GPIO_ReadBit(KNOB2_INPIN);
    knob_os = get_pin_state2(&ct, ns, knob_os);

    if (knob_os == 1)
        knobr.sta = KNOB_CLOSE;
    else
        knobr.sta = KNOB_OPEN;

    fire_R.xnsta = knobr.sta;
}
void update_leak_state(void)
{
    static u8 ct = 0;
    static u8 err_ct = 0;
    static u8 dn_ct = 0;
    volatile bool ns;
    static bool leak_os = 1;  
    ns = GPIO_ReadBit(LEAK_PIN);    
    leak_os = get_pin_state50(&ct, ns, leak_os);
	
    if (leak_os == 0)
    {//机壳电压过高，使得引脚被拉低
        shell_err_f = 1;
        dn_ct = 0;
        if(system_100ms_f)
        {
            if (++err_ct > 100)
            {//2s
                err_ct = 100;
                shell_leak_err = true;
            }
        } 		
    }
    else
    {
		shell_leak_err = false;
        shell_err_f = 0;
        err_ct = 0;        
    }
}
/* 水箱 */
void update_waterbox_state(void)
{
	static u8 ct = 0;
    volatile bool ns;
	static bool sw1_os = 1;
    static bool presta = 0;
    ns = GPIO_ReadBit(WATER_BOX_INPIN);
    sw1_os = get_pin_state2(&ct,ns,sw1_os);

    if (sw1_os == 1)    //高电平水箱推入   不插器件默认水箱推入
        waterbox.sta = BOX_CLOSE;
    else
        waterbox.sta = BOX_OPEN;
    if((presta != waterbox.sta)&& (presta == 1))
    { 
        if( gb_DryFireFlag)
        {
             gb_DryFireFlag = 0;
             gu8_AddWaterTime1 = 45;  
             gu8_AddWatterdelay = 60;          
        }
    }
    presta = waterbox.sta;

}
/*********************************************************
函数名: get_average_ad
描  述: ADC采样数据读取(2次采样平均值)(软件控制)
输入值: 采样通道
输出值: 无
返回值: 平均值
**********************************************************/
u16 get_average_ad(u8 ch)
{
    u8  i;
    u16 max, min, sum, ad_temp;

    max = 0x00;
    min = 0x0FFF;
    sum = 0x00;

    ADC->CHS.CHS = ch;                                      //ADCCHS:bit0-4 ADC模拟通道选择

    for (i = 0; i < 4; i++)
    {
        ADC_SoftStart();                                    //软件开启采样
        ADC_SoftStop();                                     //软件停止采样

        while (ADC_GetIFStatus(ADC_IF) == RESET);           //转换时间，等待采样结果

        ad_temp = ADC_GetConvValue();
        ADC_ClearIFStatus(ADC_IF);

        if (ad_temp > max)
        {
            max = ad_temp;
        }

        if (ad_temp < min)
        {
            min = ad_temp;
        }

        sum += ad_temp;
        delay_10us(5);
    }

    sum -= min;
    sum -= max;

    return sum >> 1;
}
/************************************************/
/************************************************/
/*****************热电偶**************************/
/************************************************/
/************************************************/
static void update_fire_L_current(void)
{
    static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad_current;
    static u16 thcp_ad;
    u16 v;

    ad_current = get_average_ad(THCP1_CH);

    val[ct & 0x3] = ad_current;
    if (ct < 3)
    {
        thcp_ad = ad_current;
    }
    else
    {
        v = val[0] + val[1] + val[2] + val[3];

        thcp_ad = v >> 2; //除4

        ct = (ct & 0x0f) + 0x10;                   // 永不溢出 永 > 3 
    }
    ct++;

	fire_L_thcp_ad = thcp_ad;
    fire_L.thcp_ad = fire_L_thcp_ad;
}
static void update_fire_R_current(void)
{
    static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad_current;
    static u16 thcp_ad;
    u16 v;

    ad_current = get_average_ad(THCP2_CH);

    val[ct & 0x3] = ad_current;
    if (ct < 3)
    {
        thcp_ad = ad_current;
    }
    else
    {
        v = val[0] + val[1] + val[2] + val[3];
        thcp_ad = v >> 2; //除4

        ct = (ct & 0x0f) + 0x10;                   // 永不溢出 永 > 3 
    }
    ct++;

	fire_R_thcp_ad = thcp_ad;
    fire_R.thcp_ad = fire_R_thcp_ad;
}
/* 防火墙温度转换 */
static u8 get_wall_temperature(u16 ad)
{
	u8 i;
    if(ad >= 4085)
        return 0xff;
    if ((ad >= AD_0_C)||(ad <= 5)) //0 ~ -10
        return 0;

    if (ad <= AD_MAX_C)
        return 130;

    for (i = 0; i < AD_TOTAL_N;)
    {
        if (ad > NTC_ad_table[i + 10])
            break;
        else
            i += 10;
    }

    while (ad < NTC_ad_table[i])
        i++;

    return i;
}

/* 防火墙1温度采样 */
void update_wall1_tempr(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 wall1_ad = 0;
    static u16 wall1_temp = 0;
	u16 v;

	wall1_temp = get_average_ad(WALL1AD_CH);

	val[ct & 0x3] = wall1_temp;

	if (ct < 3)
	{
		wall1_ad = wall1_temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		wall1_ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
	ct++;
	
	tempr_wall_L = get_wall_temperature(wall1_ad);
    wall_L.tempr = tempr_wall_L;
}

/* 防火墙2温度采样 */
void update_wall2_tempr(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 wall2_ad = 0;
    static u16 wall2_temp = 0;
	u16 v;

	wall2_temp = get_average_ad(WALL2AD_CH);

	val[ct & 0x3] = wall2_temp;

	if (ct < 3)
	{
		wall2_ad = wall2_temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		wall2_ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
	ct++;

    tempr_wall_R = get_wall_temperature(wall2_ad);
    wall_R.tempr = tempr_wall_R;
}
/* 干烧温度转换 */
static u16 get_gans_temperature(u16 ad)
{
	u16 i;

    if ((ad >= 4085)) //开路
        return 795;
    if(ad <= 5)//短路
        return 790;
    if (ad >= 4080)
        return 1;
    if (ad <= 124)
        return 350;
    return 0;
/*     for (i = 0; i < 249;)
    {
        if (ad > GANS_ad_table[i + 10])
            break;
        else
            i += 10;
    }

    while (ad < GANS_ad_table[i])
        i++;
    if(i>= 301)
        i = 300+((i%10)*5);

    return i; */
}
/* 干烧温度采样 */
void update_gans1_tempr(void)
{
	static u8 gct = 0;
    static u16 gval[4] = {0,0,0,0};
    static u16 gans1_ad = 0;
    static u16 gans1_temp = 0;
    static u8 delytime = 0;
	u16 v;
    gans1_temp = get_average_ad(FGS1_CH);   
    gval[gct & 0x3] = gans1_temp;  
    if(gct < 3)
    {
        gans1_ad = gans1_temp;
    }
    else
    {
        v = gval[0] + gval[1] + gval[2] + gval[3];
        gans1_ad = v >> 2;

        gct = (gct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
    }
    gct++;

    gans_L.tempr = get_gans_temperature(gans1_ad);
    gans1_AD = gans1_ad;
/*     if(system_100ms_f)
    {
        if(sta_Lfire && (knobl.sta == KNOB_OPEN))
        {
            u8_up(&delytime);
            if(delytime >= 30)
            {
                if(gans_L.tempr == 795 || gans_L.tempr == 790)
                {
                    u8_up(&gans_errcnt);
                    if(gans_errcnt >= 20)
                    {
                        gsnstemper_err = 1;
                    }
                }
                else
                {
                    gans_errcnt = 0;
                }
            }
        }
        else
        {
            delytime = 0;
            gans_errcnt = 0;
            if(knobl.sta == KNOB_CLOSE)
                gsnstemper_err = 0;                           
        }
    } */
}
/* void update_gans2_tempr(void)
{
	static u8 gct = 0;
    static u16 gval[4] = {0,0,0,0};
    static u16 gans2_ad = 0;
    static u16 gans2_temp = 0;
	u16 v;
    gans2_temp = get_average_ad(FGS2_CH);   
    gval[gct & 0x3] = gans2_temp;  
    if(gct < 3)
    {
        gans2_ad = gans2_temp;
    }
    else
    {
        v = gval[0] + gval[1] + gval[2] + gval[3];
        gans2_ad = v >> 2;

        gct = (gct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
    }
    gct++;

    gans_L.tempr = get_gans_temperature(gans2_ad);
} */

/* 熄火电容采样 */
void update_capl_state(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
    static u8 cfg_ct = 0;
	u16 v;

//	temp = get_average_ad(CAP1_CH);

	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
	ct++;

    if (ad > CAP_ON_AD)
    {
        if(++cfg_ct > 5)
        {//500ms
            cfg_ct = 5;
            fireoff_L.cap_flag = 1;
        }
    }else
    {
        fireoff_L.cap_flag = 0;
        cfg_ct = 0;
    }
    
}

/* 熄火电容采样 */
void update_capr_state(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
    static u8 cfg_ct = 0;
    u16 v;

//	temp = get_average_ad(CAP2_CH);

	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
	ct++;
    if (ad > CAP_ON_AD)
    {
        if (++cfg_ct > 5)
        { //500ms
            cfg_ct = 5;
            fireoff_R.cap_flag = 1;
        }
    }
    else
    {
        fireoff_R.cap_flag = 0;
        cfg_ct = 0;
    }
}


/* 翻板电机电流采样 */
void update_board_state(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 board1_ad = 0;
    static u16 board1_temp = 0;
	u16 v;

	board1_temp = get_average_ad(BOARD_CH);

	val[ct & 0x3] = board1_temp;

	if (ct < 3)
	{
		board1_ad = board1_temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		board1_ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
	ct++;

    board.cur_i_10mA = (u32)board1_ad  * 1000 / 4096 ;//(u32)board1_ad * 5 * 1000 / 4096 / 5; // I(A) = (5 * ad / 4096) / R(采样电阻) = ad * 5 / 0.51 / 4096
}


/* NTC温度转换 */
static u16 get_bottomntc_temperature(u16 ad)
{
    u16 i;
    u16 out = 0;

    if (ad >= 4080) //短路
        return 0xfff0;

    if (ad <= 10) //开路
        return 0xffff;

    for (i = 0; i < 189;)
    {
        if (ad < bottom2_ad_table[i + 10])//
            break;
        else
            i += 10;
    }
    if(ad > 3878)
        i = 200;
    else
    {
        while (ad >bottom2_ad_table[i])//
            i++;
    }

    out = i;//i + (float)(R200_ad_table[i]  - ad) / (R200_ad_table[i] - R200_ad_table[i + 1]);
    return out;
}
static u16 get_bakentc_temperature(u16 ad ,u8 chang)
{
    u16 i;
    u16 out = 0;

    if (ad >= 4050) //短路
        return 0xfff0;

    if (ad <= 20) //开路
        return 0xffff;
    
    if(chang)
    {
        for (i = 0; i < 190;)
        {
            if (ad < R510or10K_ad_table[i + 10])
                break;
            else
                i += 10;
        }
        if(ad > 3608)
            i = 200;
        else
        {
            while (ad > R510or10K_ad_table[i])
                i++;
        } 
        i=i+100-4;//实际测出来会比理论高4度
    }
    else
    {
        for (i = 0; i < 130;)
        {
            if (ad < R10K_ad_table[i + 10])
                break;
            else
                i += 10;
        }
        if(ad > 3565)
            i = 130;
        else
        {
            while (ad > R10K_ad_table[i])
                i++;
        }        
    }


    out = i;//
    return out;
}
bool temper_errcheck(u8 *errcnt, u16 temper)
{
    bool err = 0;
    if((temper == 0xfff0) || (temper == 0xffff))
    {
        u8_up(errcnt);
        if(*errcnt >= 10)
        {
            *errcnt = 10;
            err = 1;
        }
    }
    else
    {
        u8_dn(errcnt);
        if(*errcnt == 0)
        {
            err = 0;
        }             
    }
    return err; 
    
}
/* 箱体温度 */
void update_box_tempr(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
    static u8 chang_f = 0;
	u16 v;
    temp = get_average_ad(ZBOX_TEMPR_CH);
    if(chang_f == 0)
    {
        if(temp >3515) //125度
        {           
            chang_f = 1;
            gpio_set(ADJUST1_BOM_PIN,chang_f);  //输出0          
            delay_10us(10);
            ct = 0;
            temp = get_average_ad(ZBOX_TEMPR_CH);            
        }
    }
	else
    {
        if(temp < 671) //110度
        {
            if(chang_f)
            {
                chang_f = 0;
                gpio_set(ADJUST1_BOM_PIN,chang_f);  //输入  
                delay_10us(10);
                ct = 0; 
                temp = get_average_ad(ZBOX_TEMPR_CH);                 
            }
        }
    }

	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
	ct++;

    tempr_box.cur = get_bakentc_temperature(ad,chang_f);//
    if(system_100ms_f)
    {
        boxtempr_err = temper_errcheck(&boxerr_cnt,tempr_box.cur);
    } 
}

/* 箱体上温度 */
void update_top_tempr(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
    static u8 chang_f = 0;
	u16 v;

	temp = get_average_ad(KTOP_TEMPR_CH);
    if(chang_f == 0)
    {
        if(temp >3515) //125度
        {           
            chang_f = 1;
            gpio_set(ADJUST2_BOM_PIN,chang_f);  //输出0  
            delay_10us(10);
            ct = 0;
            temp = get_average_ad(KTOP_TEMPR_CH);            
        }
    }
	else
    {
        if(temp < 671) //110度
        {
            if(chang_f)
            {
                chang_f = 0;
                gpio_set(ADJUST2_BOM_PIN,chang_f);//输入
                delay_10us(10);
                ct = 0; 
                temp = get_average_ad(KTOP_TEMPR_CH);                 
            }
        }
    }
	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
    ct++;
    tempr_top.cur = get_bakentc_temperature(ad,chang_f);
    if(system_100ms_f)  
        toptempr_err = temper_errcheck(&toperr_cnt,tempr_top.cur);
}

/* 底盘 */
void update_bom_tempr(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
	u16 v;

	temp = get_average_ad(ZBOM_TEMPR_CH);

	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
    ct++;
    tempr_bom.cur = get_bottomntc_temperature(ad);
    if(system_100ms_f)
        bomtempr_err = temper_errcheck(&bomerr_cnt, tempr_bom.cur);

        
}

/***************************************************************************************
* Function Name  : 
* Description    :  //防火墙报警计算
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void get_firewall_up(Wall_t *wall) //200ms
{
	static u8 speed_limit = 3;	//灵敏度
	
	static u8 power_tmr = 30;       //(30*200ms)/2 = 3s
    if (power_tmr)//||wall->fireup_delay
    {
        u8_dn(&wall->fireup_delay);
        u8_dn(&power_tmr);
        wall->preTempr = wall->tempr;
        wall->temper_ct = 0;
		wall->speed_tmr = 0;
        wall_L.on_f = 0;
		wall_R.on_f = 0;
        wall->arrct = 0;
        wall->start_f = 0;        
    }
//	speed_limit = (cmd_wallDown) ? 3 : 6;
    if(wall->tempr < 35)
    {
        wall->arrct = 0;
        wall->start_f = 0;
    }
	/********************速度计算****************************/
    wall->temperarr[wall->arrct] = wall->tempr;
    wall->preTempr = (wall->arrct == (TSAMPLE_N-1))?wall->temperarr[0]:wall->temperarr[wall->arrct+1];
    ++wall->arrct;
    if(wall->arrct >= TSAMPLE_N )
    {
        wall->arrct = 0;
        wall->start_f = 1;
    }
    if(wall->start_f)
    {
        if(wall->tempr > wall->preTempr) 
        {
            wall->upSpeed = wall->tempr - wall->preTempr;
            if ((wall->upSpeed >= speed_limit)&&(wall->upSpeed <= 30)&&(wall->tempr >= 45)) //防止信号线断掉又恢复
            {
                wall->speed_tmr++;
                if(wall->speed_tmr >= 3)
                {
                    if(wall->on_f == 0)
                        wall->errTempr = wall->tempr;
					wall->on_f = true;
					wall->speed_tmr = 0;
					// wall->upSpeed = 0;
					wall->preTempr = wall->tempr;                    
                }
            }
			else
			{
				wall->speed_tmr = 0;
				// wall->upSpeed = 0;
			}            
        }  
		else
			wall->speed_tmr = 0;     
    }
    else
    {
		wall->speed_tmr = 0;     
    }

	/********************************************************/

	 if (wall->tempr > 90)
	 {
		 u8_up(&wall->temper_ct);
		 if(wall->temper_ct > 5) //5*200 = 1s
		 {	
			 wall->on_f = true;
             wall->errTempr = 90;
		 }
	 }else
	 {
		 wall->temper_ct = 0;
	 }
	 if (wall->tempr == 0xff)//
	 {
	    wall->temper_ct = 0;
	 	wall->speed_tmr = 0;
	 	wall->upSpeed = 0;
	 	wall->preTempr = wall->tempr;
        //wall->on_f = false;
	 	
	 }
    //  if((wall->tempr < (wall->errTempr - 10))&& (wall->tempr < 80) && wall->on_f)
    //  {
	//      wall->temper_ct = 0;
	//  	 wall->speed_tmr = 0;
	//  	 wall->upSpeed = 0;         
    //      wall->on_f = false;
    //  }
	/********************************************************/
}
#define SAMDRY_N    20
/***************************************************************************************
* Function Name  : 
* Description    :  干烧判断
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void dryfire_check(void)
{
/*  static u16 htemper_cnt = 0;
    static u16 htemper_cnttwo = 0;
    static u8 add_cnt = 0;
    static u8 add_cnttwo = 0;
    static bool gb_start = 0;
    static bool gb_starttwo = 0;
    static u16  ct = 0;
    static u16  cttwo = 0;
    static u8  pot_noct = 0;
    if(system_100ms_f)
        u8_dn(&gans_L.cdly);
    if(gb_ganscheckon == 0) 
    {
        gans_L.error = 0;
        return;
    }   
           
    if((knobl.sta == KNOB_OPEN)&&sta_Lfire)
    {
        if(potstate.sta == POT_NO)
        {
            if(system_sec_f)
            {
                pot_noct++;
                if(pot_noct >= 120)//有火情况下无锅120S,熄火报警
                {
                    pot_noct = 0;
                    gans_L.error = 1;
                }
            }            
        }
        else
            pot_noct = 0;  

        if(potstate.sta == POT_NO)//无锅时温升计数清零
        {
            gb_start = 0;
            gb_starttwo = 0;
            cttwo = 0;
            ct = 0;
        }
                         
        if(gans_L.tempr == 790 || gans_L.tempr == 795)
        {
            ct = 0;
            cttwo = 0;
            gb_start = 0;   
            gb_starttwo = 0;                
            return;
        }    

        if(system_sec_f)
        {
            if((gans_L.tempr >= 240)&&(gb_start == 0))
            {
                u16_up(&ct);
                if(ct >= 3600)
                {
                    gb_start = 1;
                    gans_L.etemper1 = 250;
                }

            }
            else if(gans_L.tempr < 240)
            {
                ct = 0;
                gb_start = 0;
            }

            if((gans_L.tempr >= 250)&&(gb_starttwo == 0))
            {
                u16_up(&cttwo);
                if(cttwo>= 45)
                {
                    gb_starttwo = 1;
                    if(gans_L.tempr >= 275)
                    {
                        gans_L.etemper2 = 290;
                    }
                    else
                    {
                        gans_L.etemper2 = 315;
                    }
                }
                else
                {
                    gb_starttwo = 0;
                }
            }
            else if(gans_L.tempr <250)
            {
                cttwo = 0;
                gb_starttwo = 0;
            }
        }
        //判断点1
        if(gb_start &&(gans_L.tempr >= gans_L.etemper1))
        {
            add_cnt++;
            if(add_cnt >= 3)
            {
                gans_L.error = 1;
            }
        }
        else
            add_cnt = 0;
        //判断点2
        if(gb_starttwo &&(gans_L.tempr >= gans_L.etemper2))
        {
            add_cnttwo++;
            if(add_cnttwo >= 3)
            {
                gans_L.error = 1;
            }
        }
        else
            add_cnttwo = 0;
        //判断点3
        if((gans_L.tempr >= 325)&&(potstate.sta == POT_IN))//有锅
        {
            htemper_cnttwo++;
            if(htemper_cnttwo >= 5)
            {
                gans_L.error = 1;
            }
        }
        else
            htemper_cnttwo = 0;
        //判断点4
        if((gans_L.tempr >= 340))//无锅状态下温度达到340度熄火
        {     
            htemper_cnt++;
            if(htemper_cnt >= 5)//连续500ms秒检测到过高
            {
                htemper_cnt = 5;
                gans_L.error = 1;
            }
        }
        else
            htemper_cnt = 0;

    }
    else
    {
        ct = 0;	
		cttwo = 0;		
        pot_noct = 0;
        htemper_cnt = 0;
        htemper_cnttwo = 0;
        add_cnt = 0;
        add_cnttwo = 0;        
        gb_start = 0;
        gb_starttwo = 0;  

    }
    if(knobl.sta == KNOB_CLOSE)
    {
        gans_L.error = 0;      
    } */

}
void makesoup(void)
{
/*     static u16 broth_arg[90] = {0};
    static u8 b_arrct = 0;
    static u8 dly_trm = 0;
    static u8 b_cnt = 0;
    static u8 h_cnt = 0;
    s16 speed = 0;
    
    if(sta_Lfire&&(potstate.sta == POT_IN))
    {
        if(system_sec_f )
        {
            u8_up(&dly_trm);
            broth_arg[b_arrct] = gans_L.tempr;
            gans_L.pretempr = (b_arrct == (BROTH_N-1))?broth_arg[0]:broth_arg[b_arrct+1];
            ++b_arrct;
            if(b_arrct >= BROTH_N)
            {
                b_arrct = 0;
            }
            if(dly_trm > 90)
            {
                speed = gans_L.tempr - gans_L.pretempr;
            }            
            if(boli.keepwarm_f == 0)
            {
                if((dly_trm >= 60*3)&&(gans_L.tempr > 110))//开始3分钟不判断
                {
                    if((speed <= 3)&&(speed >= -3))//3
                    {
                        b_cnt++;
                        if(b_cnt >= 60)
                        {
                            boli.keepwarm_f = 1;
                            boli.lowfire_f = 1;                         
                        }
                    }
                    else
                    {
                        b_cnt = 0;
                    }
                }
                else
                    b_cnt = 0;
                h_cnt = 0;
            }
            else
            {
                b_cnt = 0;
                if(speed <= (-10))
                {
                    h_cnt++;
                    if(h_cnt >= 10)
                    {
                        h_cnt = 0;
                        boli.keepwarm_f = 0;
                        dly_trm = 60*2;
                    }
                }
                else
                {
                    h_cnt = 0;
                }                
            }
        }
    }
    else
    {
        h_cnt = 0;
        b_cnt = 0;
        dly_trm = 0;
        b_arrct = 0;
        boli.keepwarm_f = 0;
        boli.lowfire_f = 0;
    }
    //大小火控制逻辑
    if(system_sec_f)
    {        
        if(boli.keepwarm_f)
        {
            boli.lowfire_f = 1;
        }
        else
        {
            boli.lowfire_f = 0;
        }
    }
    if(boli.soupwork_f == 0)
    {
        boli.lowfire_f = 0;
    }           */
}
/**************************************************************************************/
void init_all_inputs(void)
{

}


void update_all_inputs(void)
{
    update_knob1_state();
    update_knob2_state();

    update_door_state();
    update_waterbox_state();
    update_doorbake_state();
    update_leak_state();
    update_pot_state();

    update_fire_L_current();
    update_fire_R_current();
 
    fire_L.sw_dn = fireoff_L.cmd_flag;
    fire_R.sw_dn = fireoff_R.cmd_flag; 
    if(!shell_leak_err)
    {    
        sta_Lfire = fire_calculate(&fire_L);
        sta_Rfire = fire_calculate(&fire_R);       
        
    } 
    if((sta_Lfire == 0)||(knobl.sta == KNOB_CLOSE))	
    {
        sta_Lfire = 0;
        kmosl_firoff_f = 0;
        fireoff_L.cmd_flag = 0;
    }
       
    if((sta_Rfire == 0)||(knobr.sta == KNOB_CLOSE))
    {
        sta_Rfire = 0;
        fireoff_R.cmd_flag = 0;
        kmosr_firoff_f = 0;
    }    
    update_board_state();       
    if(system_100ms_f)
    {
        
        update_box_tempr();
        update_top_tempr();
        update_bom_tempr();

        update_wall1_tempr();
        update_wall2_tempr();

        //update_gans1_tempr();
        //dryfire_check();
        //makesoup();

    }
    if(system_200ms_f)
    {
        get_firewall_up(&wall_L);
        get_firewall_up(&wall_R);
		//ganstemperdisp();
    }

}


void ganstemperdisp(void)
{
    gans_L.diptempr = gans_L.tempr;
/*     static u8 upct = 0;
    static u8 dnct = 0;
    static u8 gens_timect = 5;
    if(gens_timect)
    {
        if(system_sec_f)
            gens_timect--;
        gans_L.diptempr = gans_L.tempr;
    }
    else
    {
        if(gans_L.tempr > gans_L.diptempr)
        {

            if(gans_L.tempr > (gans_L.diptempr+2))
            {
                gans_L.diptempr = gans_L.tempr;
            }
            else
            {
                if(system_sec_f)
                    upct++;
                if(upct >= 20)
                {
                    gans_L.diptempr++;
                    upct = 0;
                }
            }           
        }
        else
        {
            upct = 0;
        }
        if(gans_L.tempr < gans_L.diptempr)
        {
            if(gans_L.tempr <= 50)
            {
                if((gans_L.diptempr-gans_L.tempr) >= 8)
                {
                    gans_L.diptempr--;
                    dnct = 0;
                }
                else
                {
                    if(system_sec_f)
                    {
                        dnct++;
                    }
                    if( dnct >= 20)
                    {
                        gans_L.diptempr--;
                        dnct = 0;
                    }
                }
            }
            else
            {
                if((gans_L.diptempr - gans_L.tempr) > 2)
                {
                    gans_L.diptempr = gans_L.tempr;
                    dnct = 0;
                }
                else
                {
                    if(system_sec_f)
                    {
                        dnct++;
                    }
                    if( dnct >= 20)
                    {
                        gans_L.diptempr--;
                        dnct = 0;
                    }                    
                }
            }
        }
        else
        {
            dnct = 0;
        }
    }    
    if(gsnstemper_err)
    {
        gens_timect = 5;
        gans_L.diptempr = gans_L.tempr;
    }
    else
    {
        if(gans_L.diptempr == 795)
            gans_L.diptempr = 0;
        else if(gans_L.diptempr == 790)
            gans_L.diptempr = 350;
    } */
}




